#include"data_transfer.h"

void store_output(volatile Ot* out,Ot obuffer[Tm][MAX_LEN*MAX_LEN],int ofsize,int ch_out,int pool){
#pragma HLS RESOURCE variable=obuffer core=RAM_2P_BRAM
	//int addr=ch_out*ofsize*ofsize;              //��ʼ��ַ
	if(pool==0){
		for(int m=0;m<Tm;m++){
			for(int i=0;i<ofsize*ofsize;i++){
#pragma HLS LOOP_TRIPCOUNT min=144 max=144 avg=144
#pragma HLS PIPELINE II=1
				*(out+(ch_out+m)*ofsize*ofsize+i)=obuffer[m][i];
			}
		}
	}
	else{
		int count=0;
		//��Ҫ����2x2���ػ�
		for(int m=0;m<Tm;m++){
#pragma HLS LOOP_FLATTEN off
			int addr=0;
		    for(int i=0;i<ofsize*ofsize/4;i++){
#pragma HLS PIPELINE II=2
#pragma HLS LOOP_TRIPCOUNT min=36 max=36 avg=36
		    	Ot data0_0=obuffer[m][addr];
		    	Ot data0_1=obuffer[m][addr+1];
		    	Ot max1=(data0_0>data0_1)?data0_0:data0_1;
		    	Ot data1_0=obuffer[m][addr+ofsize];
		    	Ot data1_1=obuffer[m][addr+ofsize+1];
		    	Ot max2=(data1_0>data1_1)?data1_0:data1_1;
		    	*(out+(ch_out+m)*ofsize*ofsize/4+i)=(max1>max2)?max1:max2;
		    	//addr+=2;
		    	if(count==ofsize/2-1){                    //����һ��
		    		addr+=(ofsize+2);
		    		count=0;
		    	}
		    	else{
		    		addr+=2;
		    		count+=1;
		    	}

		    }
		}
	}
}


void load_weight(const Wt weight[M][N][K][K],Wt w[Tm][Tn][K][K],int ch_in,int ch_out){
	//load weight[ch_out:ch_out+Tm][ch_in:ch_in+Tn][:][:]
	for(int m=0;m<Tm;m++)
		for(int n=0;n<Tn;n++)
			for(int i=0;i<K;i++)
			  for(int j=0;j<K;j++){
#pragma HLS PIPELINE II=1
				  w[m][n][i][j]=weight[ch_out+m][ch_in+n][i][j];
			}
}
